package com.atguigu4.exer;

import com.atguigu3.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.Scanner;

public class Exer2Test {
    //删除：优化
    @Test
    public void testDelete2(){
        System.out.println("请输入考证号：");
        Scanner scan = new Scanner(System.in);
        String ExamCard = scan.next();
        String sql = "delete from examstudent where ExamCard = ?";
        int updateCount = update(sql, ExamCard);
        if(updateCount > 0){
            System.out.println("删除成功");
        }else {
            System.out.println("查无此人请重新，进入输入");
        }
    }



    //删除：原始
    @Test
    public void testDelete() {
        System.out.println("请输入考证号：");
        Scanner scan = new Scanner(System.in);
        String ExamCard = scan.next();
        String sql1 = "select Type,IDCard,ExamCard,StudentName,Location,Grade from examstudent where ExamCard = ?";
        Student instance = getInstance(Student.class, sql1, ExamCard);
        if (instance == null) {
            System.out.println("查无此人请重新，进入输入");
        } else {
            String sql = "delete from examstudent where ExamCard = ?";
            int updateCount = update(sql, ExamCard);
            if (updateCount > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }
    }


    //查询
    @Test
    public void testOrder() {
        System.out.println("请输入你要输入的类型：");
        System.out.println("a.准考证号");
        System.out.println("b.身份证号");
        Scanner scan = new Scanner(System.in);
        String selection = scan.next();
        if ("a".equalsIgnoreCase(selection)) {//selection.equalsIgnoreCase("a")
            System.out.println("请输入准考证号");
            String ExamCard = scan.next();
            String sql = "select Type,IDCard,ExamCard,StudentName,Location,Grade from examstudent where ExamCard = ?";
            Student instance = getInstance(Student.class, sql, ExamCard);
            if (instance != null) {
                System.out.println("========查询结果是========");
                System.out.println(instance);
            } else {
                System.out.println("查无此人请重新进入程序");
            }
        } else if ("b".equalsIgnoreCase(selection)) {//selection.equalsIgnoreCase("b")
            System.out.println("请输入身份证号：");
            String IDCard = scan.next();
            String sql = "select Type,IDCard,ExamCard,StudentName,Location,Grade from examstudent where IdCard = ? ";
            Student instance = getInstance(Student.class, sql, IDCard);
            if (instance != null) {
                System.out.println("========查询结果是========");
                System.out.println(instance);
            } else {
                System.out.println("查无此人请重新进入程序");
            }
        } else {
            System.out.println("输入错误，请在a,b中选择，请重新进入程序");
        }
    }

    //添加
    @Test
    public void testInsert() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入学生的Type：");
        int Type = scan.nextInt();
        System.out.println("请输入学生的IDCard：");
        String IDCard = scan.next();
        System.out.println("请输入学生的ExamCard：");
        String ExamCard = scan.next();
        System.out.println("请输入学生的StudentName：");
        String StudentName = scan.next();
        System.out.println("请输入学生的Location：");
        String Location = scan.next();
        System.out.println("请输入学生的Grade：");
        int Grade = scan.nextInt();

        String sql = "insert into examstudent(Type,IDCard,ExamCard,StudentName,Location,Grade) values(?,?,?,?,?,?)";
        int count = update(sql, Type, IDCard, ExamCard, StudentName, Location, Grade);
        if (count > 0) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public int update(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps);
        }
        return 0;
    }

    public <T> T getInstance(Class<T> clazz, String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            rs = ps.executeQuery();

            ResultSetMetaData rsmd = rs.getMetaData();

            int columnCount = rsmd.getColumnCount();

            if (rs.next()) {
                //在此使用
                T t = clazz.getDeclaredConstructor().newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = rs.getObject(i + 1);

                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t, columnValue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }
        return null;
    }
}
